package pl.matt.media.manager.impl;

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import pl.matt.media.model.ImageType;
import pl.matt.model.Rectangle;

import com.jhlabs.image.CropFilter;
import com.jhlabs.image.GrayscaleFilter;

/**
 * @author mateusz
 *
 */

public class JHLabsImageManager extends AbstractImageManager {

	@Override
	public boolean cropImage(String sourceImagePath, String destinationImagePath, int x, int y, int width, int height,
			ImageType imageType) throws IOException {
		CropFilter cropFilter = new CropFilter(x, y, width, height);
		return filter(cropFilter, sourceImagePath, destinationImagePath, imageType);	
	}

	@Override
	public boolean convertToGrayScale(String sourceImagePath, String destinationImagePath, ImageType imageType) throws IOException {
		GrayscaleFilter filter = new GrayscaleFilter();
		return filter(filter, sourceImagePath, destinationImagePath, imageType);		
	}
	
	private boolean filter(BufferedImageOp imageOp, String sourceImagePath, String destinationImagePath, ImageType imageType) throws IOException {
		BufferedImage image = ImageIO.read(new File(sourceImagePath));
		image = imageOp.filter(image, null);
		return ImageIO.write(image, imageType.getFormatName(), new File(destinationImagePath));
	}

	@Override
	public void divideImage(String sourceImagePath, String destinationImagePath1, String destinationImagePath2,
			boolean horizontal, ImageType imageType) throws IOException {
		throw new IllegalArgumentException("unimplemented");
		
	}

	@Override
	public boolean cropImageWithPercentageDimensions(String sourceImagePath, String destinationImagePath,
			int xPercentage, int yPercentage, int widthPercentage, int heightPercentage, ImageType imageType)
			throws IOException {
		throw new IllegalArgumentException("unimplemented");
	}

	@Override
	public Rectangle getImageRectangle(File imageFile) throws IOException {
		throw new IllegalArgumentException("unimplemented");
	}

}
